package com.thomas.sharing.mybatis.controller;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thomas.sharing.mybatis.dao.CarDao;
import com.thomas.sharing.mybatis.mapper.CarMapper;
import com.thomas.sharing.mybatis.model.Brand;
import com.thomas.sharing.mybatis.model.Displacement;
import com.thomas.sharing.mybatis.model.PriceResult;
import com.thomas.sharing.mybatis.model.Product;
import com.thomas.sharing.mybatis.request.PriceRequest;

@Controller
public class CarController {		
	
	private Log LOG = LogFactory.getLog(CarController.class);
	
	@Autowired
	private CarMapper carMapper;
	
	@Autowired
	@Qualifier("carDaoMybatis")
	private CarDao carDao;
	
	@Autowired
	@Qualifier("carDaoJdbcTemplate")
	private CarDao carDaoImpl;
		
	@RequestMapping(value ={"", "/"}, method=RequestMethod.GET)
	public String home(Model model) throws InstantiationException, IllegalAccessException{
		PriceRequest priceRequest = new PriceRequest();
		model.addAttribute("carPriceRequest", priceRequest);
		this.brands(model);
		this.products(priceRequest, model);
		this.displacements(priceRequest, model);
		return "carPrice";
	}
	
	@PostMapping("/brands")
	@ResponseBody 
	public List<Brand> brands(Model model) throws InstantiationException, IllegalAccessException{
		List<Brand> brands = carMapper.getBrands();
		model.addAttribute("brandList", brands);
		return brands;
	}
	
	@PostMapping("/products")
	@ResponseBody 
	public List<Product> products(PriceRequest priceRequest, Model model){
		LOG.info("brands: " + priceRequest.getBrands());	
		List<Product> productsToChose = this.carDao.getProductsByBrands(priceRequest.getBrands());
		//priceRequest.setProducts(productsToChose.stream().map(product -> product.getName()).collect(Collectors.toList()));
		model.addAttribute("productList", productsToChose);
		productsToChose.forEach(LOG::info);
		return productsToChose;
	}
	
	@PostMapping("/displacements")
	@ResponseBody 
	public List<Displacement> displacements(PriceRequest priceRequest, Model model){
		List<Displacement> displacementsToChose = 
				this.carDao.getDisplacementByBrandsAndProducts(priceRequest.getBrands(), priceRequest.getProducts());
		model.addAttribute("displacementList", displacementsToChose);
		LOG.info("displacementsToChose: " + displacementsToChose);
		return displacementsToChose;
	}		
	
	@PostMapping("/prices")
	@ResponseBody 
	public List<PriceResult> prices(PriceRequest priceRequest){
		List<PriceResult> prices = this.carDao.getPrices(priceRequest);
		//List<PriceResult> prices = this.carDaoImpl.getPrices(priceRequest);
		LOG.info("prices: " + prices);
		return prices;
	}		
}
